<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二分查找</title>
</head>
<body>
    <script>
        function binarySearch(arr,ret) {
            let left = 0;
            let right = arr.length-1;
            let mid = 0;
            while(left<=right) {
                mid = left + (right-left)/2;
                if(arr[mid] == ret) {
                    break;
                }else if(arr[mid]>ret) {
                    right = mid-1;
                }else {
                    left = mid+1;
                }
            }
            return mid;
        }
        let arr = [1,2,3,4,5,6,7];
        console.log(binarySearch(arr,5));

    </script>
</body>
</html>